package com.resurvolution.kingcloudmusicspringboot.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resurvolution.kingcloudmusicspringboot.common.constant.ResultConstant;
import com.resurvolution.kingcloudmusicspringboot.common.constant.UserConstant;
import com.resurvolution.kingcloudmusicspringboot.common.exception.FileException;
import com.resurvolution.kingcloudmusicspringboot.common.utils.BaseContext;
import com.resurvolution.kingcloudmusicspringboot.common.utils.UnifyResult;
import com.resurvolution.kingcloudmusicspringboot.product.dto.FavoriteSongDTO;
import com.resurvolution.kingcloudmusicspringboot.product.dto.PlayListDTO;
import com.resurvolution.kingcloudmusicspringboot.product.dto.SongsDTO;
import com.resurvolution.kingcloudmusicspringboot.product.entity.SongsEntity;
import com.resurvolution.kingcloudmusicspringboot.product.mapper.SongsMapper;
import com.resurvolution.kingcloudmusicspringboot.product.service.*;
import com.resurvolution.kingcloudmusicspringboot.product.vo.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/songs")
public class SongsController {

    @Resource
    private SongsService songsService;

    @Resource
    private GenreService genreService;

    @Resource
    private PlayListService playListService;
    @Resource
    private FavoriteService favoriteService;

    @Resource
    private SongsMapper songsMapper;

    @PostMapping("/uploadSongs")
    public UnifyResult<String> uploadSongs( @RequestPart(value = "lyric", required = false) MultipartFile lyric,
                             @RequestPart(value = "tinyImg", required = false) MultipartFile tinyImg,
                             @RequestPart(value = "largeImg", required = false) MultipartFile largeImg,
                             @RequestPart("song") MultipartFile song,
                             @RequestPart("songInfo") String songDataJson) {
        try {
            songsService.uploadSongs(song, lyric, tinyImg, largeImg, songDataJson);
            return UnifyResult.success(ResultConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            log.error("上传出错", e);
            throw new FileException();
        }

    }

    @GetMapping("/genres")
    public UnifyResult<List<GenreVO>> genres() {
        return UnifyResult.success(genreService.getGenres());
    }

    @GetMapping("/carousels")
    public UnifyResult<List<CarouselsVO>> carousels() {
        return UnifyResult.success(songsService.getCarouselsList());
    }

    @GetMapping("/songsPage")
    public UnifyResult<PageVO<SongsPageVO>> songsPage(
            @RequestParam Integer page,
            @RequestParam Integer size,
            @RequestParam String sortBy,
            @RequestParam String sortOrder,
            @RequestParam(required = false) Integer genreId
    ) {
        return UnifyResult.success(songsService.getPage(page, size, sortBy, sortOrder, genreId));
    }

    @GetMapping("/song")
    public UnifyResult<SongsVO> song(@RequestParam Integer id) {
        return UnifyResult.success(songsService.getSongById(id));
    }

    @PostMapping("/insertSongToPlayList")
    public UnifyResult<String> insertSongToPlayList(@RequestBody PlayListDTO playListDTO) {
        return UnifyResult.success(playListService.insertPlayList(playListDTO));
    }

    @PostMapping("/playList")
    public UnifyResult<List<PlayListVO>> playList() {
        return UnifyResult.success(playListService.getPlayListByUserId(BaseContext.getCurrentId()));
    }

    @PostMapping("/love")
    public UnifyResult<Boolean> love(@RequestBody FavoriteSongDTO favoriteSongDTO) {
        return UnifyResult.success(favoriteService.insertLoveSong(favoriteSongDTO.getId()));
    }

    @GetMapping("/love")
    public UnifyResult<Boolean> love(@RequestParam Integer id) {
        return UnifyResult.success(favoriteService.selectSongBySongId(id));
    }

    @GetMapping("/recommend")
    public UnifyResult<List<RecommendSongsVO>> recommend(@RequestParam Integer songId) {
        return UnifyResult.success(songsService.getRecommendSongList(songId));
    }

    @GetMapping("/favorite")
    public UnifyResult<PageVO<SongsPageVO>> favorite(@RequestParam Integer page,
                                                     @RequestParam Integer size,
                                                     @RequestParam String sortBy,
                                                     @RequestParam String sortOrder) {
        return UnifyResult.success(favoriteService.getFavoriteSongs(page, size, sortBy, sortOrder, BaseContext.getCurrentId()));
    }
}
